﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;

namespace Database
{
    public class GalaxyData
    {
        public static void UpdateFilm(Galaxy film)
        {
            FilmContainer db = new FilmContainer();
            var f = (from p in db.Galaxies where p.Url == film.Url select p).FirstOrDefault();
            if (f == null)
            {
                db.Galaxies.Add(film);
            }
            else
            {
                f = film;
            }
            db.SaveChanges();
            var lst = LayURLThongTinGheNgoi(film.Url);
            if (lst.Count > 0)
            {
                HtmlDocument ds = new HtmlDocument();
                HtmlWeb hw = new HtmlWeb();
                foreach (var str in lst)
                {
                    ds = hw.Load(str);
                    var d = ds.DocumentNode.SelectNodes("//div[@class='book-info-content-item-content']");
                    var lc = new LichChieuGalaxy();
                    lc.GalaxyId = film.Id;
                    lc.Rap = d[1].ChildNodes[1].InnerHtml;
                    lc.Ngaychieu = d[2].ChildNodes[1].InnerHtml;
                    lc.Giochieu = d[3].ChildNodes[1].InnerHtml;
                    lc.Phongchieu = d[4].ChildNodes[1].InnerHtml;
                    lc.Url = str;
                    var l = (from p in db.LichChieuGalaxies where p.Url == lc.Url select p).FirstOrDefault();
                    if (l == null)
                    {
                        db.LichChieuGalaxies.Add(lc);
                    }
                    else
                    {
                        l = lc;
                    }

                    db.SaveChanges();
                }

            }
        }
        public static List<string> LayURLThongTinGheNgoi(string url)
        {
            HtmlDocument ds = new HtmlDocument();
            HtmlWeb hw = new HtmlWeb();
            ds = hw.Load(url);

            var d = ds.DocumentNode.SelectNodes("//a[@class='mov_sc_hour btnShowtime']");
            var lst = new List<string>();
            if (d != null)
            {
                foreach (var i in d)
                {
                    lst.Add(i.Attributes[1].Value);
                }
            }
            //if (lst.Count > 0)
            //{
            //    foreach (var str in lst)
            //    {
            //        ds = hw.Load(str);
            //        d = ds.DocumentNode.SelectNodes("//div[@class='book-info-content-item-content']");
            //        var j = d[2].ChildNodes[1].InnerHtml;
            //    }
            //}
            return lst;
        }
        public static List<Galaxy> LayDsFilm()
        {
            var db = new FilmContainer();
            DateTime today = DateTime.Now;
    
            var d = (from p in db.Galaxies  select p).ToList();
            var list = new List<Galaxy>();
            foreach(var f in d)
            {
                if (DateTime.Compare(today.Date, LayNgay(f.Khoichieu)[1].Date) == -1 || DateTime.Compare(today.Date, LayNgay(f.Khoichieu)[1].Date) == 0)
                {
                    list.Add(f);
                }
            }
            return list;
        }

        public static List<DateTime> LayNgay(string khoichieu)
        {
            var d = new List<DateTime>();
            var bd = "";
            var kt = "";
            var str = "";
            foreach (var i in khoichieu)
            {
                if (i == '0' || i == '1' || i == '2' || i == '3' || i == '4' || i == '5' || i == '6' || i == '7' || i == '8' || i == '9')
                {
                    str = str + i;
                }
            }
            bd = str.Substring(0, 8);
            kt = str.Substring(8, 8);
            var year = Int32.Parse(bd.Substring(4, 4));
            var month = Int32.Parse(bd.Substring(2, 2));
            var day = Int32.Parse(bd.Substring(0, 2));

            DateTime ngaybd = new DateTime(year, month, day);

            year = Int32.Parse(kt.Substring(4, 4));
            month = Int32.Parse(kt.Substring(2, 2));
            day = Int32.Parse(kt.Substring(0, 2));

            DateTime ngaykt = new DateTime(year, month, day);
            d.Add(ngaybd.Date);
            d.Add(ngaykt.Date);

            return d;
        }
        
    }
}
